/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import model.Location;

/**
 *
 * @author William
 */
public class LocationDao {

    public LocationDao() {
    }

    public void create(Location location, EntityManager em) {
        try {
            em.persist(location);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public Location search(Long id, EntityManager em) {
        Location location = null;
        try {
            location = em.find(Location.class, id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return location;
    }

    public void update(Location location, EntityManager em) {
        try {
            em.merge(location);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void delete(Location location, EntityManager em) {
        try {
            em.remove(location);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public List<Location> listAll(EntityManager em) {
        List<Location> locationList = new ArrayList<>();
        Query q = em.createQuery("SELECT u FROM Location u");
        try {
            List<Location> locations = new ArrayList<Location>();
            locations = q.getResultList();
            for (Object s : locations) {
                locationList.add((Location) s);
            }
            return locationList;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return locationList;
    }
}